import 'package:flutter/material.dart';
import 'package:flutter_lui/flutter_lui.dart';
import 'package:flutter_lui/src/l_style.dart';

class LLoadingMask {
  static Color _bgColor = Colors.black.withOpacity(0.4);
  static Color _foundColor = Colors.black.withOpacity(0.4);
  static Color _textColor = Colors.white;
  static double _textSize = LStyle.fontSize;
  static double _foundWidth = lrpx(100);
  static double _foundHeight = lrpx(100);
  static String _tips = '加载中...';
  static Color _progressBgColor = LUI.instance.color9;
  static Color _progressFoundColor = Colors.white;
  static double _progressStrokeWidth = lrpx(2);
  static OverlayEntry? _overlayEntry;

  /// 初始化 \
  /// @param bgColor 背景色 \
  /// @param foundColor 前景颜色 \
  /// @param foundWidth 前景宽度 \
  /// @param foundHeight 前景高度 \
  /// @param canClose 可被关闭 \
  /// @param textColor 提示文字颜色 \
  /// @param textSize 提示文字字号 \
  /// @param progressBgColor 加载圆圈背景色 \
  /// @param progressFoundColor 加载圆圈前景色 \
  /// @param progressFoundColor 加载圆圈前景色 \
  /// @param progressStrokeWidth 加载圆圈线条宽度 \
  /// @param tips 提示内容
  static init(
      {
      /// 背景色
      Color? bgColor,
      Color? foundColor,
      double? foundWidth,
      double? foundHeight,
      bool? canClose,
      Color? textColor,
      double? textSize,
      Color? progressBgColor,
      Color? progressFoundColor,
      double? progressStrokeWidth,
      String? tips}) {
    _bgColor = bgColor ?? _bgColor;
    _foundColor = foundColor ?? _foundColor;
    _foundWidth = foundWidth ?? foundHeight ?? _foundWidth;
    _foundHeight = foundHeight ?? foundWidth ?? _foundHeight;
    _tips = tips ?? _tips;
    _textColor = textColor ?? _textColor;
    _textSize = textSize ?? _textSize;
    _progressBgColor = progressBgColor ?? _progressBgColor;
    _progressFoundColor = progressFoundColor ?? _progressFoundColor;
    _progressStrokeWidth = progressStrokeWidth ?? _progressStrokeWidth;
  }

  static hide() {
    _overlayEntry?.remove();
    _overlayEntry = null;
  }

  static show({String? tips, bool? canClose = false}) {
    hide();
    var useTips = tips ?? _tips;
    _overlayEntry = getOverlayEntry(useTips, canClose == true);
    if (LNavigatorObserver.currentRoute?.navigator?.overlay != null) {
      LNavigatorObserver.currentRoute?.navigator!.overlay!
          .insert(_overlayEntry!);
    }
  }

  static OverlayEntry getOverlayEntry(String tips, canClose) {
    return OverlayEntry(
      builder: (context) => GestureDetector(
        onTap: canClose == true ? hide : null,
        child: Material(
            color: _bgColor,
            child: Center(
              child: LBox(
                  width: _foundWidth,
                  height: _foundHeight,
                  backgroundColor: _foundColor,
                  borderRadius: [LStyle.borderRadius],
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    SizedBox(
                      width: lrpx(40),
                      height: lrpx(40),
                      child: CircularProgressIndicator(
                        strokeWidth: _progressStrokeWidth,
                        backgroundColor: _progressBgColor,
                        valueColor:
                            AlwaysStoppedAnimation<Color>(_progressFoundColor),
                      ),
                    ),
                    Container(height: lrpx(10)),
                    LText(tips, fontSize: _textSize, color: _textColor)
                  ]),
            )),
      ),
    );
  }
}
